package net.myspring.common.utils;

import com.sohu.idcenter.IdWorker;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by liuj on 2016-08-10.
 */
@Component
public class SnowflakeUtils {
    public static Long workerId;
    public static Long datacenterId;

    private static IdWorker idWorker;

    public static IdWorker getIdWorker() {
        if (idWorker == null) {
            idWorker = new IdWorker(workerId, datacenterId, 0L);
        }
        return idWorker;
    }

    @Value("${idworker.worker-id}")
    public void setWorkerId(Long workerId) {
        SnowflakeUtils.workerId = workerId;
    }

    @Value("${idworker.datacenter-id}")
    public void setDatacenterId(Long datacenterId) {
        SnowflakeUtils.datacenterId = datacenterId;
    }

    public static Long getId() {
        return getIdWorker().getId();
    }
}
